package vip.xianz.jobmanager.web.controller.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import vip.xianz.jobmanager.data.entity.XUser;
import vip.xianz.jobmanager.data.vo.ResultBean;
import vip.xianz.jobmanager.service.IXUserService;
import vip.xianz.jobmanager.util.CheckUtils;
import vip.xianz.jobmanager.util.SessionUtils;

import javax.validation.Valid;
import java.time.Instant;

/**
 * 用户个人信息的访问：查看个人信息及修改
 *
 * @author xian5
 * @create 2019 -05 -09  15:47
 */
@Controller
@RequestMapping(value = "/user")
public class UserInfoController {
    @Autowired
    private IXUserService userService;

    @GetMapping(value = "/info")
    public @ResponseBody ResultBean<XUser> userInfo() {
        String userId = SessionUtils.getUser().getUserId();
        XUser userInfo = userService.getUserWithBaseInfo(userId);
        return new ResultBean<XUser>(userInfo);
    }

    @PostMapping(value = "/updateInfo")
    public @ResponseBody ResultBean<Boolean> updateInfo(@Valid XUser user, BindingResult errors) {
        CheckUtils.notErrors(errors);
        user.setUpdateTime(Instant.now());
        return new ResultBean<Boolean>(userService.updateUser(user));
    }
}
